<?php

// +----------------------------------------------------------------------
// | ShintianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2011 Shintian All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: ljt <ljt1002@gmail.com>
// +----------------------------------------------------------------------
class MsgAction extends BaseAction {

    protected function checkUser() {
        if (!isset($_SESSION[C('USER_AUTH_KEY')])) {
            $this->assign('jumpUrl', PHP_FILE . C('USER_AUTH_GATEWAY'));
            $this->error(L("_NOT_LOGIN_"));
        }
    }

    function index() {
        $this->checkUser();
        $map = array();
        $map ['toId'] = $_SESSION [C('USER_AUTH_KEY')];
        $map['status'] = 1;
        if (method_exists($this, '_filter')) {
            $this->_filter($map);
        }
        $model = D('Msg');
        if (!empty($model)) {
            $this->_list($model, $map);
        }
        $this->display();
    }

    function showmsg() {
        $this->checkUser();
        $showmsgWhere = array();
        $showmsgModel = M("Msg");
        $showmsgWhere ['toId'] = $_SESSION [C('USER_AUTH_KEY')];
        $showmsgWhere ['id'] = $_GET['id'];
        $showmsgWhere ['status'] = 1;
        $showmsgnum = $showmsgModel->where($showmsgWhere)->count();
        if ($showmsgnum) {
            $showmsg = $showmsgModel->where($showmsgWhere)->find();
            $data = array();
            $data['id'] = $_GET['id'];
            $data['isRead'] = '1';
            $showmsgModel->save($data);
            $this->assign('showmsg', $showmsg);
            $this->display();
        } else {
            $this->error(L('_MSG_ERROR_'));
        }
    }

    function sendmsg() {
        $this->checkUser();
        $this->display();
    }

    function doSend() {
        $this->checkUser();
        $toId = getUserId($_POST['toUser']);
        if ($toId) {
            if ($toId == $_SESSION[C('USER_AUTH_KEY')]) {
                $this->error(L('_MSG_SEND_NOSELF_'));
            }
            if (sendMsg($_SESSION[C('USER_AUTH_KEY')], $toId, $_POST['subject'], $_POST['content'])) {
                $this->success(L('_MSG_SEND_SUCCESS_'));
            } else {
                $this->error(L('_MSG_SEND_ERROR_'));
            }
        } else {
            $this->error(L('_MSG_SEND_NOUSER_'));
        }
    }

}

?>